var router = require('../common');
const typeModule = require('../../model/typeMode');
// 添加分类
router.post('/add',(req,res)=>{
    let { name ,yzm} = req.body;
    yzm = yzm-0;  //验证码，转小写
    let reqyzm = req.session.yzm ? req.session.yzm-0:0;
    if(yzm != reqyzm){
         return res.send({
            code:2,
            msg:'验证码验证失败'
        })
    }
    // 验证通过，删除保存的验证码
    delete req.session.yzm;
    typeModule.findOne({name}).then((data)=>{
        console.log(data);
        if(!data){
            typeModule.create({name}).then((data)=>{
                console.log(data);
                return res.send({
                    code:0,
                    msg:'添加成功'
                })
            })
        }else{
            return res.send({
                code:1,
                msg:'添加失败，已有该类名'
            })
        }
    })
    // return res.send({
    //     code:1,
    //     msg:'添加失败，已有该类名'
    // })
    
})
// 查类
router.get('/',(req,res)=>{
    typeModule.find({},{__v:0}).then((data)=>{
        return res.json({
            code:0,
            data
        })
    }).catch((err)=>{
        return res.josn({
            code:1,
            msg:err
        })
    })
})
// 删除分类
router.post('/delete',(req,res)=>{
    let {id} = req.body;
    console.log(id);
    async function deleteData(){
        let data = await typeModule.deleteOne({_id:id});
        if(data.ok){
            res.send({
                code:0,
                msg:'删除成功'
            })
        }
    }
    deleteData();
    
})
// 修改是否显示
router.post('/show',async (req,res)=>{
    try {
        // console.log(req.body);
        let {id,publish} = req.body;
        publish = (publish-0) == 0 ? false : true;
        let isId = await typeModule.findOne({_id:id});
        console.log(isId);
        if(isId){
            let data = await typeModule.updateOne({_id:id},{$set:{publish}});
            res.json({
                code:0,
                msg:'设置成功'
            })
        }else{
            res.json({
                code:1,
                msg:'请刷新页面，暂时没有这个类'
            })
        }
        // res.json({
        //     code:0,
            
        // })
    } catch (err) {
        res.status(500).json({
            code:9,
            msg:err
            
        })
    }
    
})
// 修改分类名字
router.post('/setName',async (req,res)=>{
    try {
        // console.log(req.body);
        let {id,name} = req.body;
        let ncid = await typeModule.find({_id:{$ne:id}});
        if(ncid.some(item=>item.name == name)){
            return res.json({
                code:1,
                msg:'已有该类名'
            })
        }else{
            let data = await typeModule.updateOne({_id:id},{$set:{name}});
            return res.json({
                code:0,
                msg:'修改成功'
            })
        }
    } catch (err) {
        res.status(500).json({
            code:9,
            msg:err
            
        })
    }
    
})
module.exports = router;